﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Tenebrous_Manor.Navigation
{
    class DialogueMap
    {
        static int row = GameConstants.ROW_COUNT;
        static int col = GameConstants.COL_COUNT;

        public static void LoadCurrentMap(Room current_room, ref Dialogue.Dialogue_keys[,] dialogue_array)
        {
            Initialize(ref dialogue_array); //reset the dialogue map array

            switch (current_room)
            {
                case Room.MAIN_ENTRANCE:
                    MainEntranceDialogue(ref dialogue_array);
                    break;
                case Room.FOYER:
                    FoyerDialogue(ref dialogue_array);
                    break;
                case Room.JUNK_ROOM:
                    JunkRoomDialogue(ref dialogue_array);
                    break;
                case Room.KITCHEN:
                    KitchenDialogue(ref dialogue_array);
                    break;
                case Room.STUDY:
                    StudyDialogue(ref dialogue_array);
                    break;

                case Room.UPPER_LANDING:
                    UpperLandingDialogue(ref dialogue_array);
                    break;
                case Room.BEDROOM:
                    BedroomDialogue(ref dialogue_array);
                    break;

                case Room.LOWER_LANDING:
                    LowerLandingDialogue(ref dialogue_array);
                    break;
                case Room.BASEMENT:
                    BasementDialogue(ref dialogue_array);
                    break;
                case Room.RESEARCH_LAB:
                    ResearchDialogue(ref dialogue_array);
                    break;
            }
        }

        public static void Initialize(ref Dialogue.Dialogue_keys[,] array)
        {
            for (int i = 0; i <= row - 1; i++)
            {
                for (int j = 0; j <= col - 1; j++)
                {
                    array[i, j] = Dialogue.Dialogue_keys.NOTHING;
                }
            }
        }

        #region 1st floor
        public static void MainEntranceDialogue(ref Dialogue.Dialogue_keys[,] array)
        {
            try
            {
                for (int i = 0; i <= row - 1; i++)
                {
                    for (int j = 0; j <= col - 1; j++)
                    {
                        array[i, j] = Dialogue.Dialogue_keys.SAFE_FEELING;
                    }
                }

                array[4, 0] = Dialogue.Dialogue_keys.DOOR;
                array[5, 0] = Dialogue.Dialogue_keys.DOOR;

                array[4, 9] = Dialogue.Dialogue_keys.ENTRANCE_DOOR;
                array[5, 9] = Dialogue.Dialogue_keys.ENTRANCE_DOOR;

                array[1, 7] = Dialogue.Dialogue_keys.DESK;
                array[1, 8] = Dialogue.Dialogue_keys.DESK_WITH_BOOK;
            }
            catch (IndexOutOfRangeException e)
            {
                Debug.WriteLine("Main Entrance Dialogue out of bound index exception");
            }

        }

        public static void FoyerDialogue(ref Dialogue.Dialogue_keys[,] array)
        {
            try
            {
                array[0, 3] = Dialogue.Dialogue_keys.DOOR;
                array[4, 9] = Dialogue.Dialogue_keys.DOOR;
                array[5, 9] = Dialogue.Dialogue_keys.DOOR;
                array[9, 7] = Dialogue.Dialogue_keys.DOOR;

                array[4, 0] = Dialogue.Dialogue_keys.STAIRS_UP;
                array[4, 1] = Dialogue.Dialogue_keys.STAIRS_UP;
                array[5, 0] = Dialogue.Dialogue_keys.STAIRS_UP;
                array[5, 1] = Dialogue.Dialogue_keys.STAIRS_UP;
            }
            catch (IndexOutOfRangeException e)
            {
                Debug.WriteLine("Foyer Dialogue out of bound index exception");
            }
        }

        public static void KitchenDialogue(ref Dialogue.Dialogue_keys[,] array)
        {
            try
            {
                array[0, 7] = Dialogue.Dialogue_keys.DOOR;
                array[1, 1] = Dialogue.Dialogue_keys.FRIDGE;
                array[5, 1] = Dialogue.Dialogue_keys.COUNTER;
                array[6, 1] = Dialogue.Dialogue_keys.SINK;
                array[7, 1] = Dialogue.Dialogue_keys.STOVE;
                array[8, 2] = Dialogue.Dialogue_keys.COUNTER;
                array[8, 3] = Dialogue.Dialogue_keys.COUNTER;
                array[8, 4] = Dialogue.Dialogue_keys.COUNTER;

                array[5, 6] = Dialogue.Dialogue_keys.TABLE;
                array[6, 6] = Dialogue.Dialogue_keys.TABLE;
                array[5, 7] = Dialogue.Dialogue_keys.TABLE;
                array[6, 7] = Dialogue.Dialogue_keys.TABLE;

                Tuple<int, int> lever = RandomizedClueLocations.lever;
                array[lever.Item1, lever.Item2] = Dialogue.Dialogue_keys.KITCHEN_LEVER;
                
            }
            catch (IndexOutOfRangeException e)
            {
                Debug.WriteLine("Kitchen Dialogue out of bound index exception");
            }
        }

        public static void JunkRoomDialogue(ref Dialogue.Dialogue_keys[,] array)
        {
            try
            {
            for (int i = 2; i <= 7; i++)
            {
                for (int j = 2; j <= 7; j++)
                {
                    array[i, j] = Dialogue.Dialogue_keys.BUNCH_OF_JUNK;
                }
            }

            Tuple<int, int> screwdriver = new Tuple<int, int>(
                (int)((int)RandomizedClueLocations.screwdriver.Item1 / (int)GameConstants.tileSize.X),
                (int)((int)RandomizedClueLocations.screwdriver.Item2 / (int)GameConstants.tileSize.Y)
                );

            array[screwdriver.Item1, screwdriver.Item2] = Dialogue.Dialogue_keys.SCREWDRIVER;

            array[9, 2] = Dialogue.Dialogue_keys.DOOR;
            array[0, 5] = Dialogue.Dialogue_keys.STUDY_DOOR;
            }
            catch (IndexOutOfRangeException e)
            {
                Debug.WriteLine("Junk Dialogue out of bound index exception");
            }
        }

        public static void StudyDialogue(ref Dialogue.Dialogue_keys[,] array)
        {
            array[9, 5] = Dialogue.Dialogue_keys.DOOR;

            for (int i = 1; i <= 8; i++)
                array[i, 1] = Dialogue.Dialogue_keys.BOOKS;

            for (int k = 5; k <= 8; k++)
                array[k, 8] = Dialogue.Dialogue_keys.BOOKS;

            Tuple<int, int> note = RandomizedClueLocations.studyNote;

            array[note.Item1, note.Item2] = Dialogue.Dialogue_keys.NOTE;

            if (GameBools.getGameBool(GameBools.GameBool.DESK_MOVED))
            {
                array[1, 4] = Dialogue.Dialogue_keys.STUDY_DESK;
                array[1, 5] = Dialogue.Dialogue_keys.STUDY_DESK;
                array[1, 6] = Dialogue.Dialogue_keys.STUDY_DESK;
                array[2, 4] = Dialogue.Dialogue_keys.STUDY_DESK;
                array[2, 5] = Dialogue.Dialogue_keys.STUDY_DESK;
                array[2, 6] = Dialogue.Dialogue_keys.STUDY_DESK;

                array[3, 6] = Dialogue.Dialogue_keys.TRAP_DOOR;
            }
            else
            {
                array[3, 4] = Dialogue.Dialogue_keys.STUDY_DESK;
                array[3, 5] = Dialogue.Dialogue_keys.STUDY_DESK;
                array[3, 6] = Dialogue.Dialogue_keys.STUDY_DESK;
                array[2, 4] = Dialogue.Dialogue_keys.STUDY_DESK;
                array[2, 5] = Dialogue.Dialogue_keys.STUDY_DESK;
                array[2, 6] = Dialogue.Dialogue_keys.STUDY_DESK;
            }
        }
        #endregion

        #region 2nd floor
        public static void UpperLandingDialogue(ref Dialogue.Dialogue_keys[,] array)
        {
            array[4, 4] = Dialogue.Dialogue_keys.STAIR_DOWN;
            array[5, 4] = Dialogue.Dialogue_keys.STAIR_DOWN;
            array[4, 5] = Dialogue.Dialogue_keys.STAIR_DOWN;
            array[5, 5] = Dialogue.Dialogue_keys.STAIR_DOWN;

            array[8, 4] = Dialogue.Dialogue_keys.BROKEN_DOOR;

            array[0, 7] = Dialogue.Dialogue_keys.MISSING_DOORKNOB;
        }

        public static void BedroomDialogue(ref Dialogue.Dialogue_keys[,] array)
        {
            array[1, 1] = Dialogue.Dialogue_keys.CUPBOARD;
            array[2, 1] = Dialogue.Dialogue_keys.CUPBOARD;

            array[4, 1] = Dialogue.Dialogue_keys.BED;
            array[4, 2] = Dialogue.Dialogue_keys.BED;
            array[4, 3] = Dialogue.Dialogue_keys.BED;
            array[5, 3] = Dialogue.Dialogue_keys.BED;
            array[6, 2] = Dialogue.Dialogue_keys.BED;
            array[6, 3] = Dialogue.Dialogue_keys.BED;

            array[7, 1] = Dialogue.Dialogue_keys.CUPBOARD;
            array[8, 1] = Dialogue.Dialogue_keys.CUPBOARD;

            if (!GameBools.getGameBool(GameBools.GameBool.STUDY_KEY))
            {
                Tuple<int, int> key = RandomizedClueLocations.studyKey;
                array[key.Item1, key.Item2] = Dialogue.Dialogue_keys.STUDY_KEY;
            }
        }
        #endregion

        #region basement floor
        public static void LowerLandingDialogue(ref Dialogue.Dialogue_keys[,] array)
        {
            for (int i = 2; i <= 7; i++)
            {
                foreach (int j in new int[] { 2, 3, 6, 7 })
                {
                    array[i, j] = Dialogue.Dialogue_keys.BOX;
                }
            }
            array[9, 2] = Dialogue.Dialogue_keys.DOOR;

            array[8, 4] = Dialogue.Dialogue_keys.STAIRS_UP;
            array[8, 5] = Dialogue.Dialogue_keys.STAIRS_UP;
        }

        public static void BasementDialogue(ref Dialogue.Dialogue_keys[,] array)
        {
            array[2, 2] = Dialogue.Dialogue_keys.BOX;
            array[2, 3] = Dialogue.Dialogue_keys.BOX;
            array[3, 2] = Dialogue.Dialogue_keys.BOX;
            array[3, 3] = Dialogue.Dialogue_keys.BOX;

            array[6, 2] = Dialogue.Dialogue_keys.BOX;
            array[6, 3] = Dialogue.Dialogue_keys.BOX;
            array[7, 2] = Dialogue.Dialogue_keys.BOX;
            array[7, 3] = Dialogue.Dialogue_keys.BOX;

            array[6, 6] = Dialogue.Dialogue_keys.BOX;
            array[6, 7] = Dialogue.Dialogue_keys.BOX;
            array[7, 6] = Dialogue.Dialogue_keys.BOX;
            array[7, 7] = Dialogue.Dialogue_keys.BOX;

            array[1, 5] = Dialogue.Dialogue_keys.BIG_HOLE;
            array[2, 6] = Dialogue.Dialogue_keys.BIG_HOLE;
            array[3, 7] = Dialogue.Dialogue_keys.BIG_HOLE;
            array[4, 8] = Dialogue.Dialogue_keys.BIG_HOLE;

            array[9, 7] = Dialogue.Dialogue_keys.LAB_DOOR;

        }

        public static void ResearchDialogue(ref Dialogue.Dialogue_keys[,] array)
        {
            foreach (int i in new int[] { 2, 3, 6, 7 })
            {
                foreach (int j in new int[] { 1, 3, 4, 6, 7 })
                    array[i, j] = Dialogue.Dialogue_keys.MACHINE;
            }

            array[0, 7] = Dialogue.Dialogue_keys.DOOR;

            Tuple<int, int> fuse = RandomizedClueLocations.fuse;
            array[fuse.Item1, fuse.Item2] = Dialogue.Dialogue_keys.FUSE;

            Tuple<int, int> breaker = RandomizedClueLocations.breaker;
            array[breaker.Item1, breaker.Item2] = Dialogue.Dialogue_keys.BREAKER;

        }
        #endregion
    }
}
